package com.mdxq.mall.sale.controller;

import cn.hutool.core.collection.CollUtil;
import com.mdxq.mall.response.R;
import com.mdxq.mall.sale.entity.FlashPromotionSession;
import com.mdxq.mall.sale.service.FlashPromotionProductRelationService;
import com.mdxq.mall.sale.service.FlashPromotionSessionService;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Collections;
import java.util.List;

/**
 * 限时购场次表 控制层。
 *
 * @author ShuJie
 * @since 2025-01-12
 */
@RestController
@RequestMapping("/api/v1/flashSession")
public class FlashPromotionSessionController {

    @Autowired
    private FlashPromotionSessionService flashPromotionSessionService;

    /**
     * 添加限时购场次表。
     *
     * @param flashPromotionSession 限时购场次表
     * @return {@code true} 添加成功，{@code false} 添加失败
     */
    @PostMapping("save")
    public R save(@RequestBody FlashPromotionSession flashPromotionSession) {
        return flashPromotionSessionService.save(flashPromotionSession)?R.ok(true):R.ok(false);
    }

    /**
     * 根据主键删除限时购场次表。
     *
     * @param id 主键
     * @return {@code true} 删除成功，{@code false} 删除失败
     */
    @DeleteMapping("remove/{id}")
    public boolean remove(@PathVariable Long id) {
        return flashPromotionSessionService.removeById(id);
    }

    /**
     * 根据主键更新限时购场次表。
     *
     * @param flashPromotionSession 限时购场次表
     * @return {@code true} 更新成功，{@code false} 更新失败
     */
    @PutMapping("update")
    public boolean update(@RequestBody FlashPromotionSession flashPromotionSession) {
        return flashPromotionSessionService.updateById(flashPromotionSession);
    }

    /**
     * 查询所有限时购场次表。
     *
     * @return 所有数据
     */
    @GetMapping("list")
    public R list() {
        return R.ok(flashPromotionSessionService.list());
    }

    /**
     * 根据限时购场次表主键获取详细信息。
     *
     * @param id 限时购场次表主键
     * @return 限时购场次表详情
     */
    @GetMapping("getInfo/{id}")
    public FlashPromotionSession getInfo(@PathVariable Long id) {
        return flashPromotionSessionService.getById(id);
    }

    /**
     * 分页查询限时购场次表。
     *
     * @param page 分页对象
     * @return 分页对象
     */
    @GetMapping("page")
    public Page<FlashPromotionSession> page(Page<FlashPromotionSession> page) {
        return flashPromotionSessionService.page(page);
    }
    @Autowired
    private FlashPromotionProductRelationService relationService;
    @GetMapping("selectList")
    public R selectList(@RequestParam Long flashPromotionId){
        QueryWrapper queryWrapper=new QueryWrapper();
        queryWrapper.eq("flash_promotion_id",flashPromotionId);
        queryWrapper.select("flash_promotion_session_id");
        List<Long>sessionIds=relationService.listAs(queryWrapper,Long.class);
        return CollUtil.isNotEmpty(sessionIds)?R.ok(flashPromotionSessionService.listByIds(sessionIds)):R.ok(Collections.emptyList());
    }

}
